/*
* File: TaskDefConverter.java
* Author: Daniel Rogers
* Created on Jan 18, 2008
*
*/
package gri.gridp.tasks;
import gri.tasks.ParameterDef;
import gri.tasks.TaskDef;
import gri.data.Types;
import gri.data.DataType;
import gri.gridp.modules.Function;
import gri.gridp.modules.Parameter;
import gri.gridp.modules.Flag;
import gri.gridp.modules.BasicParameter;
import gri.gridp.modules.Output;
import java.util.List;
/**
* Class to create a ParameterizedTaskDef for a GRIDP function definition
* (as described in a GRIDP).
*
* @author dan.rogers
*/
public class BasicTaskDefProvider implements TaskDefProvider {
DataType FILE_TYPE = Types.FILE;
DataType FILE_LIST_TYPE = Types.createListType(List.class, Types.FILE);
/**
* Creates a ParameterizedTaskDef for the given function
*/
public TaskDef createTaskDef(Function function) {
TaskDef def = new TaskDef();
//inputs:
List funcParams = function.getParameters();
Parameter funcParam;
for (int i=0; i<funcParams.size(); i++) {
funcParam = (Parameter)funcParams.get(i);
ParameterDef taskInput = convertFunctionParameter(funcParam);
def.addInput(taskInput);
}
//outputs:
List outputs = function.getOutputs();
Output output;
for (int i=0; i<outputs.size(); i++) {
output = (Output)outputs.get(i);
ParameterDef taskOutput = convertOutput(output);
def.addOutput(taskOutput);
}
return def;
}
/**
* Creates a parameter definition (ParameterDef) for a task from a
* GRIDP parameter definition.
*/
protected ParameterDef convertFunctionParameter(Parameter param) {
//basic:
ParameterDef newParam = new ParameterDef(param.getName(), getDataType(param));
newParam.setDisplayName(param.getTitle());
newParam.setDescription(param.getHelp());
//basic parameter (options & default):
if (param instanceof BasicParameter) {
BasicParameter basicParam = (BasicParameter)param;
if (basicParam.hasDefault())
newParam.setDefaultValue(basicParam.getDefault());
if (basicParam.hasOptions()) {
List options = basicParam.getOptionList();
for (int i=0; i<options.size(); i++) {
gri.gridp.modules.Option option = (gri.gridp.modules.Option)options.get(i);
gri.tasks.Option newOption = new gri.tasks.Option(
option.getDisplayText(),
option.getValue()
);
newParam.addOption(newOption);
}
}
}
//flag (set type)
else if (param instanceof Flag) {
Flag flagParam = (Flag)param;
System.out.println(flagParam);
if (flagParam.hasDefault()) {
System.out.println("flag default: " + flagParam.getDefault());
newParam.setDefaultValue(flagParam.getDefault());
}
}
return newParam;
}
/**
* Converts an output parameter from the function to a ParameterDef
*/
protected ParameterDef convertOutput(Output output) {
//basic:
ParameterDef newParam = new ParameterDef(output.getName(), getDataType(output));
newParam.setDisplayName(output.getTitle());
newParam.setDescription(output.getHelp());
return newParam;
}
/**
* Returns the DataType for the given parameter. The representation class
* will be a String, Boolean, or TransferableFile.
*/
protected DataType getDataType(Parameter param) {
if (param instanceof BasicParameter) {
String type = ((BasicParameter)param).getParameterType();
return convertType(type);
}
else if (param instanceof Output) {
String type = ((Output)param).getType();
return convertType(type);
}
else if (param instanceof Flag)
return Types.BOOLEAN;
//should never happen:
else
throw new RuntimeException("Illegal parameter class: " + param.getClass().getName());
}
protected DataType convertType(String type) {
if (type.equals("file"))
return FILE_TYPE;
else if (type.equals("file-list"))
return FILE_LIST_TYPE;
else {
String contentType = type; //these are identical ;)
return Types.createDataType(contentType, String.class);
}
}
}